#include "factory.h"
//线程池初始化
int factory_init(Factory_t *pf,int thread_num,int capacity)
{
    que_init(&pf->que,capacity);//队列初始化
    int ret=pthread_cond_init(&pf->cond,NULL);//条件变量初始化
    THREAD_ERR_CHECK(ret,"pthread_cond_init");
    pf->pthid=(pthread_t *)calloc(thread_num,sizeof(pthread_t));//为线程id申请空间
    pf->start_flag=0;//线程池启动标志设为0
    return 0;
}
//线程池启动
int factory_start(Factory_t *pf,int thread_num)
{
    if(!pf->start_flag)//如果线程池没启动
    {
        int i;
        for(i=0;i<thread_num;i++)
        {
            pthread_create(pf->pthid+1,NULL,thread_handle,pf);
        }
        pf->start_flag=1;
    }
}

//释放锁资源
void cleanup(void* p)
{
    pthread_mutex_unlock((pthread_mutex_t*)p);
}
//子线程流程
void* thread_handle(void* p)
{
    Factory_t* pf=(Factory_t*)p;
    pQue_t pq=&(pf->que);//让一个队列指针指向队列
    pNode_t pcur=NULL;//取队列结点
    //建立puts/gets专属传输路线
    int tran_fd;
    char thread_ip[30];
    char thread_port[30];
    memset(thread_ip,0,sizeof(thread_ip));
    memset(thread_port,0,sizeof(thread_port));
    strcpy(thread_ip,"10.10.46.10");//这里要改为自己IP
    strcpy(thread_port,"2000");
    while(1)
    {
        pthread_mutex_lock(&pq->mutex);
        pthread_cleanup_push(cleanup,&pq->mutex);
        if(0==pq->que_size)//如果队列为空
        {
            pthread_cond_wait(&pf->cond,&pq->mutex);
        }
        que_get(pq,&pcur);////取队列结点
        pthread_cleanup_pop(1);//直接通过pop来解锁
        if(pcur)//pcur不为NULL代表拿到了结点
        {
            int newsocketFd=socket(AF_INET,SOCK_STREAM,0);
            int ret;
            struct sockaddr_in my_addr;
            int on=1;
            ret=setsockopt(newsocketFd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));
            memset(&my_addr,0,sizeof(struct sockaddr_in));
            my_addr.sin_family = AF_INET;
            my_addr.sin_port = htons(atoi(thread_port));
            my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
            ret=connect(tran_fd,(struct sockaddr*)&my_addr,sizeof(my_addr));
            //ERROR_CHECK(ret,-1,"connect");
            if(ret == -1)
            {
                printf("Connect fail!\n");
                break;
            }
            ret =tran_file(pcur->tran_fd,&pcur->rv_cmd);
            if(ret==-1)
            {
                printf("something leave!\n"); 
            }
            else{
                printf("Trans Success!\n");
            }
            free(pcur);
            pcur=NULL;
            close(tran_fd);
            close(newsocketFd);
            continue;
        }
    }
}